package com.wnx.job.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.modules.pms.mapper.PmsIndustryIntentionRelationMapper;
import com.wnx.job.modules.pms.model.PmsIndustry;
import com.wnx.job.modules.pms.model.PmsIndustryIntentionRelation;
import com.wnx.job.modules.rms.mapper.RmsIntentionMapper;
import com.wnx.job.modules.rms.model.RmsIntention;
import com.wnx.job.modules.rms.service.RmsIntentionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class RmsIntentionServiceImpl extends ServiceImpl<RmsIntentionMapper, RmsIntention> implements RmsIntentionService {
    @Resource
    private RmsIntentionMapper intentionMapper;
    @Resource
    private PmsIndustryIntentionRelationMapper industryIntentionRelationMapper;


    /**
     * 根据简历ID获取求职意向列表
     * @param resumeId
     * @return
     */
    @Override
    public List<RmsIntention> findIntentionListByResumeId(Long resumeId) {
        return intentionMapper.selectList(new QueryWrapper<RmsIntention>().eq("resume_id",resumeId));
    }

    /**
     * 根据ID获取行业集合
     * @param id
     * @return
     */
    @Override
    public List<PmsIndustry> findIndustryListById(Long id) {

        return intentionMapper.findIndustryListById(id);
    }

    /**
     *新增求职意向
     * @param intention
     * @param industryIds
     */
    @Override
    public void saveIntention(RmsIntention intention, List<Long> industryIds) {
        //校验：一份简历至多有三份求职意向 超过三个报异常。
        long count = this.findIntentionCountByResumeId(intention.getResumeId());
        if (count >= 3) {
            Asserts.fail("一份简历至多有三份求职意向，请先删除求职意向后，再进行添加！");

        }
        //调用持久层，进行新增求职意向
        intentionMapper.insert(intention);
        List<PmsIndustryIntentionRelation> container = new ArrayList<>();
        Long intentionId = intention.getId();
        if (industryIds != null && industryIds.size() > 0) {
            for (Long industryId : industryIds) {
                PmsIndustryIntentionRelation x = new PmsIndustryIntentionRelation();
                x.setIntentionId(intentionId);
                x.setIndustryId(industryId);
                industryIntentionRelationMapper.insert(x);
            }

        }
    }



    /**
     * 根据简历ID查询求职意向数量
     * @param resumeId
     * @return
     */
    @Override
    public long findIntentionCountByResumeId(long resumeId) {
        return intentionMapper.selectCount(new QueryWrapper<RmsIntention>().eq("resume_id",resumeId));
    }

    /**
     * 删除求职意向
     * @param id
     */
    @Override
    public void deleteIntention(Long id) {

        //调用持久层，删除行业求职关联
        long count = this.findIndustryIntentionCountById(id);
        if (count > 0){this.deleteIndustryIntentionById(id);}


        //调用持久层，根据ID删除求职意向
        intentionMapper.deleteById(id);
    }

    /**
     * 根据求职意向ID查询行业求职意向关联表数量
     * @param id
     * @return
     */
    @Override
    public long findIndustryIntentionCountById(Long id) {
        return industryIntentionRelationMapper.selectCount(new QueryWrapper<PmsIndustryIntentionRelation>().eq("intention_id",id));
    }


    /**
     * 根据ID删除行业求职意向关联
     * @param id
     */
    @Override
    public void deleteIndustryIntentionById(Long id) {
        industryIntentionRelationMapper.delete(new QueryWrapper<PmsIndustryIntentionRelation>().eq("intention_id",id));
    }
}
